package com.spring.boot.composite;

import java.util.ArrayList;
import java.util.List;

public class ShoppingTest {
    public static void main(String[] args) {
        float s ;
        Bags bigBag = new Bags("大袋子");
        Bags mediumBag = new Bags("中袋子");
        Bags smallRedBag = new Bags("红色小袋子");
        Bags smallWhiteBag = new Bags("白色小袋子");
        Goods sp ;
        //创建二个商品放到红色小袋子
        sp = new Goods("昔源特产",2,7.9f);
        smallRedBag.add(sp);
        sp = new Goods("昔源地图",1,9.9f);
        smallRedBag.add(sp);
        //再创建二个商品放到白色小袋子
        sp=new Goods("韶关香菇",2,68);
        smallWhiteBag.add(sp);
        sp=new Goods("韶关红茶",3,180);
        smallWhiteBag.add(sp);
        //第三个商品放中型袋子,红色袋子也放入
        sp=new Goods("景德镇瓷器",1,380);
        mediumBag.add(sp);
        mediumBag.add(smallRedBag);
        sp=new Goods("李宁牌运动鞋",1,198);
        bigBag.add(sp);
        bigBag.add(smallWhiteBag);
        bigBag.add(mediumBag);
        System.out.println("您选购的商品有：");
        bigBag.show();
        s=bigBag.calculation();
        System.out.println("要支付的总价是："+s+"元");

    }
}

//抽象构件：物品
interface Articles
{
    public float calculation(); //计算
    public void show();
}
//树叶构件：商品
class Goods implements Articles {
    private String name;     //名字
    private int quantity;    //数量
    private float unitPrice; //单价

    public Goods(String name, int quantity, float unitPrice) {
        this.name = name;
        this.quantity = quantity;
        this.unitPrice = unitPrice;
    }

    public float calculation(){
        return quantity * unitPrice;
    }

    public void show() {
        System.out.println("数量:"+quantity + ",单价:"+unitPrice);
    }
}
//树枝构件：袋子
class Bags implements Articles{

    private String name;
    private List<Articles> bags = new ArrayList<Articles>();
    public Bags(String name) {
        this.name=name;
    }

    public void add(Articles c){
        bags.add(c);
    }

    public void remove(Articles c){
        bags.remove(c);
    }

    public Articles getChild(int i){
        return bags.get(i);
    }

    public float calculation(){
        float s = 0;
        for (Articles bag : bags) {
            s += bag.calculation();
        }
        return s;
    }

    public void show() {
        for (Articles bag : bags) {
            bag.show();
        }
    }
}
